import type { HasId } from "@/types/common";
import type { IRentMachineOrderDetailVO, IRentMachineOrderInfoList, IRentreceiptDTO } from "@/types/rentMachineManage/rentMachineOrderInfo";
import { getRentMobileOrderByIdInfo } from "@/utils/api/rentMobileOrder";

/* 上传图片 */
function useReceipt(toggleState: (status: boolean) => void, fn?: () => void) {
  const receiptVisible = ref(false);
  const receiptForm = ref<IRentreceiptDTO>({
    id: 0,
    receipt: "",
    customer_known_pic: "",
    contract_address: ""
  });

  /* 打开上传图片 模态框*/
  function handleUploadReceipt(row: IRentMachineOrderInfoList) {
    receiptForm.value.id = row.id;
    receiptForm.value.receipt = row.receipt;
    receiptForm.value.customer_known_pic = row.customer_known_pic;
    receiptForm.value.contract_address = "";
    toggleState(true);
    getRentMobileOrderByIdInfo<IRentMachineOrderDetailVO, HasId>({ id: row.id })
      .then(res => {
        receiptVisible.value = true;
        if (res.result.is_receipt_signed == 1) {
          if (res.result.order_receipt_esign) {
            receiptForm.value.contract_address = res.result.order_receipt_esign.contract_address;
          }
        }
      })
      .catch(() => {})
      .finally(() => {
        toggleState(false);
      });
  }

  /* 上传回执单，告客户知情同意书成功回调 */
  function handleReceiptSuccess() {
    receiptVisible.value = false;
    if (fn) {
      fn();
    }
  }

  return {
    receiptVisible,
    receiptForm,
    handleUploadReceipt,
    handleReceiptSuccess
  };
}

export default useReceipt;
